package com.naiterui.ehp.bs.cms.modules.wms.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.cms.modules.wms.feign.IMedicineFeignClient;
import com.naiterui.ehp.bs.cms.modules.wms.service.IMedicineService;
import com.naiterui.ehp.bs.cms.modules.wms.vo.MedicineVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * <pre>
 *      商品：药品
 * </pre>
 *
 * @author 王泽浩
 * @date 2019-06-11 15:46
 * @since 1.0.0
 */
@Slf4j
@Validated
@RestController
@Api(tags = {SwaggerTag.CMS + SwaggerTag.MEDICINE_DRUG})
@RequestMapping("wms/medicine")
public class MedicineController {

  @Autowired
  private IMedicineFeignClient medicineFeignClient;
  @Autowired
  private IMedicineService medicineService;

  /**
   * 商品信息
   *
   * @return
   */
  @SysLog("商品详情:药品类")
  @GetMapping("/drug/info/{productId}")
  @ApiOperation(value = "药品详情")
  @ApiImplicitParam(name = "productId", value = "商品ID", dataType = "long", paramType = "path", required = true)
  @PreAuthorize("@p.check('wms:medicine:info')")
  public MedicineVO info(@PathVariable("productId") Long productId) {
    log.info("商品详情:药品类 请求参数：productId = {}", productId);
    return this.medicineService.getMedicineById(productId);
  }

  @SysLog("保存修改商品:药品")
  @PostMapping("/drug/update")
  @ApiOperation(value = "修改商品")
  @PreAuthorize("@p.check('wms:medicine:update')")
  public Long updateDetails(@Valid @RequestBody MedicineVO medicineVO) throws BusinessException {
    log.info("保存修改商品:药品 请求参数：medicineVO = {}", medicineVO);
    return this.medicineFeignClient.saveOrUpdate(medicineVO);
  }

}
